social.admin = new liwe.module ( 'social.admin', null, 'Social' );

social.admin.init = function ()
{
	if ( ! social.templates )
	{
		social.load_templates ( social.admin.init );
		return;
	}
	social.admin._read_config ( function ()
	{
		social.admin.register (
		[
			{
				func: social.admin._manage_tokens,
				label: 'Gestione tokens'
			},
			{
				func: social.admin._manage_pages,
				label: 'Gestione pagine'
			},
			{
				func: social.admin._manage_actions,
				label: 'Gestione azioni'
			},
			{
				func: social.admin._manage_post,
				label: 'Post (test)'
			}
		] );
	} );
};

// {{{ MANAGE

social.admin._read_config = function ( cback )
{
	social.admin.ajax ( { action: 'social.admin.read_config' }, function ( res )
	{
		social.admin._settings = res.settings;
		cback ();
	} );
};

social.admin._manage_tokens = function ()
{
	//$( 'system-content', social.templates [ 'admin-user-tokens' ] );
	var tpl = {
		title: 'Gestione tokens',
		title_left: 'Azioni',
		title_right: 'Lista tokens',
		left: String.formatDict ( social.templates [ 'admin-user-tokens-left' ] ),
		right: String.formatDict ( social.templates [ 'admin-user-tokens-right' ] )
	};
	$( 'system-content', String.formatDict ( social.templates [ 'admin-layout' ], tpl ) );
	social.admin._token_list ();
};

social.admin._manage_pages = function ()
{
	//$( 'system-content', social.templates [ 'admin-pages' ] );
	var tpl = {
		title: 'Gestione pagine',
		title_left: 'Aggiungi pagina',
		title_right: 'Lista pagine',
		left: String.formatDict ( social.templates [ 'admin-pages-left' ] ),
		right: String.formatDict ( social.templates [ 'admin-pages-right' ] )
	};
	$( 'system-content', String.formatDict ( social.templates [ 'admin-layout' ], tpl ) );
	social.admin._page_list ();
	social.admin.facebook_page_list ();
};

social.admin._manage_actions = function ()
{
	var tpl = {
		title: 'Gestione azioni',
		title_left: 'Aggiungi azione',
		title_right: 'Lista azioni',
		left: String.formatDict ( social.templates [ 'admin-actions-left' ] ),
		right: String.formatDict ( social.templates [ 'admin-actions-right' ] )
	};
	$( 'system-content', String.formatDict ( social.templates [ 'admin-layout' ], tpl ) );
	social.admin._action_list ();
	social.admin._form_action ();
};

social.admin._manage_post = function ()
{
	$( 'system-content', social.templates [ 'admin-post' ] );
	social.admin._post_page_list ();
	social.admin._form_post ();
};

// }}}

// {{{ TOKENS

social.admin._token_list = function ()
{
	social.admin.ajax ( { action: 'social.admin.token_list' }, function ( res )
	{
		var html = '';
		if ( res.list )
		{
			res.list.iterate ( function ( v )
			{
				html += String.formatDict ( social.templates [ 'admin-user-token-item' ], v );
			} );
		}
		$( 'social-user-token-list', html );
	} );
};

social.admin.token_add = function ( service )
{
	var url = String.formatDict ( social.templates [ service + '-token-url' ], social.admin._settings [ service ] );
	window.showModalDialog ( url, null, 'dialogWidth:1024px;dialogHeight:600px;center:yes' );
	social.admin._token_list ();
};

social.admin.token_delete = function ( uid, service )
{
	if ( ! confirm ( 'Cancellare il token?' ) ) return;
	social.admin.ajax ( { action: 'social.admin.token_delete', uid: uid, service: service }, function ( res )
	{
		liwe.utils.notifier.show ( 'Token cancellato!', 'info', 3000 );
		social.admin._token_list();
	} );
};

// }}}

// {{{ POST

social.admin.facebook_post_list = function ( page_id )
{
	social.facebook.post_list ( page_id, function ( posts )
	{
		var html = '', params;
		posts.iterate ( function ( v )
		{
			params = {
				url: v [ 'id' ].replace ( '_', '/posts/' ),
				message: v [ 'story' ] ? v [ 'story' ] : v [ 'message' ],
				timestamp: v [ 'created_time' ]
			};
			html += String.formatDict ( social.templates [ 'admin-facebook-status' ], params );
		} );
		$( 'social-facebook-post-list', html );
	} );
};

social.admin._post_page_list = function ()
{
	social.admin.ajax ( { action: 'social.admin.page_list' }, function ( res )
	{
		var html = '';
		if ( res.list )
		{
			html += String.formatDict ( social.templates [ 'admin-post-page-item' ], { id: '', label: 'Facebook App' } );
			res.list.iterate ( function ( v )
			{
				html += String.formatDict ( social.templates [ 'admin-post-page-item' ], v );
			} );
		}
		$( 'social-post-pages', html );
	} );
};

// }}}

//  {{{ PAGE

social.admin._page_list = function ()
{
	social.admin.ajax ( { action: 'social.admin.page_list' }, function ( res )
	{
		var html = '';
		if ( res.list )
		{
			res.list.iterate ( function ( v )
			{
				html += String.formatDict ( social.templates [ 'admin-page-item' ], v );
			} );
		}
		$( 'social-page-list', html );
	} );
};

social.admin.page_add = function ( page_id, label )
{
	var params = {
		page_id: page_id,
		label: label,
		action: 'social.admin.page_save',
		service: 'FACEBOOK'
	};
	social.admin.ajax ( params, function ( res ) {
		if ( res.status != 'OK' )
		{
			liwe.utils.notifier.show ( res.status, 'alert', 3000 );
			return;
		}
		liwe.utils.notifier.show ( 'Salvataggio effettuato!', 'info', 3000 );
		social.admin._page_list ();
	} );
};

social.admin.page_delete = function ( page_id, service )
{
	if ( ! confirm ( 'Cancellare la pagina?' ) ) return;
	var params = {
		page_id: page_id,
		action: 'social.admin.page_delete',
		service: service
	};
	social.admin.ajax ( params, function ( res )
	{
		liwe.utils.notifier.show ( 'Pagina cancellata!', 'info', 3000 );
		social.admin._page_list();
	} );
};

social.admin.facebook_page_list = function ()
{
	social.admin.ajax ( { action: 'social.admin.facebook_page_list' }, function ( res )
	{
		var html = '';
		if ( ! res.list ) return;
		res.list.iterate ( function ( v )
		{
			html += String.formatDict ( social.templates [ 'admin-facebook-page' ], v );
		} );
		$ ( 'social-page-add', html );
	} );
};

// }}}

// {{{ ACTIONS

social.admin._action_list = function ()
{
	social.admin.ajax ( { action: 'social.admin.action_list' }, function ( res )
	{
		var html = '';
		if ( res.list )
		{
			res.list.iterate ( function ( v )
			{
				html += String.formatDict ( social.templates [ 'admin-action-item' ], v );
			} );
		}
		$( 'social-action-list', html );
	} );
};

social.admin.action_add = function ( service )
{
	var url = String.formatDict ( social.templates [ service + '-action-url' ], social.admin._settings [ service ] );
	window.showModalDialog ( url, null, 'dialogWidth:1024px;dialogHeight:600px;center:yes' );
	social.admin._action_list ();
};

social.admin.action_delete = function ( name )
{
	if ( ! confirm ( 'Cancellare l\'azione?' ) ) return;
	social.admin.ajax ( { action: 'social.admin.action_delete', name: name }, function ( res )
	{
		liwe.utils.notifier.show ( 'Azione cancellata!', 'info', 3000 );
		social.admin._action_list();
	} );
};

// }}}

// {{{ FORMS

social.admin._form_post = function ()
{
	social.admin.ajax ( { action: 'social.admin.page_list' }, function ( res )
	{
		var f = new liwe.form.instance ( 'facebook-post' );
		var pages = [ { label: '(app)', value: '' } ];
		if ( res.list )
		{
			res.list.iterate ( function ( page )
			{
				pages.push ( { label: page.label, value: page.id } );
			} );
		}
		f.select ( { label: 'Pagina', name: 'page_id', options: pages } );
		var impersopt = [
			{ label: 'Me stesso', value: 'me' },
			{ label: 'Pagina', value: 'page' }
		];
		f.select ( { label: 'Come', name: 'impersonate', options: impersopt } );
		f.textarea ( { label: 'Testo', name: 'text' } );
		f.button ( { value: 'Invia', 'class': 'green', onclick: 'social.admin.facebook_do_post()' } );
		f.set ( 'social-post-add' );
	} );
};

social.admin._form_action = function ()
{
	social.admin.ajax ( { action: 'social.admin.page_list' }, function ( res )
	{
		if ( ! res.list ) return;
		var f = new liwe.form.instance ( 'action' );
		var pages = [];
		res.list.iterate ( function ( page )
		{
			pages.push ( { label: page.label, value: page.id } );
		} );
		f.select ( { label: 'Pagina', name: 'page_id', options: pages } );
		f.text ( { label: 'Azione', name: 'name' } );
		f.button ( { value: 'Invia', 'class': 'green', onclick: 'social.admin._action_do_post()' } );
		f.set ( 'social-action-add' );
	} );
};

// }}}

// {{{ FORMS CALLBACKS

social.admin.facebook_do_post = function ()
{
	var f = liwe.form.get ( 'facebook-post' );
	var params = f.get_values ();
	params [ 'action' ] = 'social.admin.facebook_post';
	social.admin.ajax ( params, function ( res ) {
		if ( res.status != 'OK' )
		{
			liwe.utils.notifier.show ( res.status, 'alert', 3000 );
			return;
		}
		liwe.utils.notifier.show ( 'Post effettuato', 'info', 3000 );
		social.admin.post_list ( params [ 'page_id' ] );
	} );
};

social.admin._action_do_post = function ()
{
	var f = liwe.form.get ( 'action' );
	var params = f.get_values ();
	params [ 'service' ] = 'FACEBOOK';
	params [ 'action' ] = 'social.admin.action_save';
	social.admin.ajax ( params, function ( res ) {
		if ( res.status != 'OK' )
		{
			liwe.utils.notifier.show ( res.status, 'alert', 3000 );
			return;
		}
		liwe.utils.notifier.show ( 'Azione creata', 'info', 3000 );
		social.admin._action_list ();
	} );
};

// }}}
